<template>
  <PageWrapper class="virtual-scroll-demo">
    <a-space>
      <a-input v-model:value="sn" @change="snChange" />
      <a-input v-model:value="topic" @change="topicChange" />
      <a-button type="primary" @click="subscribe">监听</a-button>
      <a-button type="primary" @click="closeWs">关闭连接</a-button>
    </a-space>
    <div class="mb-2"></div>

    <div class="text-center mb-4">
      <a-button @click="vScrollRef?.scrollToTop()">滚动到顶部</a-button>
      <a-button @click="vScrollRef?.scrollToBottom()" class="mx-2">滚动到底部</a-button>
      <a-button @click="vScrollRef?.scrollToItem(scrollToItemIndex)"
        >滚动到第
        <input-number
          v-model:value="scrollToItemIndex"
          class="!w-60px mx-1"
          :min="1"
          :max="list.length"
          :precision="0"
          size="small"
          :controls="false"
          @keydown.enter="vScrollRef?.scrollToItem(scrollToItemIndex)"
        />
        条
      </a-button>
    </div>
    <div class="virtual-scroll-demo-wrap">
      <VScroll :itemHeight="150" :items="list" :height="800" :width="100" ref="vScrollRef">
        <template #default="{ item }">
          <div class="virtual-scroll-demo__item">
            <div v-html="item"></div>
          </div>
        </template>
      </VScroll>
    </div>
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { PageWrapper } from '@/components/Page';
  import { VScroll } from '@/components/VirtualScroll';
  import { InputNumber } from 'ant-design-vue';
  import { ref } from 'vue';
  import { Persistent } from '@/utils/cache/persistent';
  import { SN_KEY, WS_TOPIC_KEY } from '@/enums/cacheEnum';
  import { disconnectWs, getStompClient } from '@/utils/ws';
  import { object } from 'vue-types';

  const vScrollRef = ref<typeof VScroll>();
  const scrollToItemIndex = ref(1000);

  const list = ref([]);

  let sn = ref<string>(Persistent.getLocal(SN_KEY) || '');
  let topic = ref<string>(Persistent.getLocal(WS_TOPIC_KEY) || '/v1/sn/${sn}/iotData');

  function snChange() {
    Persistent.setLocal(SN_KEY, sn.value);
  }

  function topicChange() {
    Persistent.setLocal(WS_TOPIC_KEY, topic.value);
  }

  async function subscribe() {
    let stompClient = await getStompClient();
    // stompClient.send('/ws/subscribe/v1/sn/{sn}/realtimeLog', {}, payload);
    const subscribeTopic = topic.value.replace('${sn}', sn.value);
    console.log('subscribeTopic ', subscribeTopic);
    stompClient.subscribe(subscribeTopic, function (message) {
      console.log('subscribe have ', message);
      const json = JSON.parse(message.body);
      let line = '';
      for (let k in json) {
        const item = json[k];
        let str = '';
        if (typeof item == object) {
          str = JSON.stringify(item);
        } else {
          str = item;
        }
        line = line + `<span style="color: red">${k}</span>:<span>${str}</span></br>`;
      }
      list.value.push(line);
    });
  }

  function closeWs() {
    disconnectWs();
  }
</script>
<style lang="less" scoped>
  .virtual-scroll-demo {
    &-wrap {
      display: flex;
      justify-content: center;
      background-color: @component-background;
    }

    &__item {
      height: 150px;
      padding: 0;
      border-bottom: 1px solid @border-color-base;
      line-height: 20px;
    }
  }
</style>
